package com.deke.student.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deke.entity.TbMember;
import com.deke.student.dao.TbMenberDao;
import com.deke.student.service.TbMenberService;
import com.deke.student.utils.RedisUtils;
import com.deke.student.vo.QianLoginVo;
import com.deke.student.vo.TbMenberVo;
import com.deke.student.vo.UpTbMenberVo;
import com.deke.utils.JwtTool;
import com.deke.utils.RandomCode;
import com.deke.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.bouncycastle.cms.RecipientId.password;

/**
 * @author ：陈昂（731712841@qq.com）
 * @date ：Created in 2022/12/6 12:20
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class TbMenberServiceImpl implements TbMenberService {
    @Autowired
    private TbMenberDao tbMenberDao;

    @Override
    public Result<IPage<TbMember>> queryByConditionPage(Integer currentPage, Integer pageSize, TbMenberVo tbMenberVo) {
        IPage<TbMember> page = new Page(currentPage, pageSize);
        QueryWrapper<TbMember> wrapper = new QueryWrapper<>();
        // 模糊查询,手机号 molie是否有值,如果有就加上这个条件
        if(StringUtils.hasText(tbMenberVo.getMobile())){
            wrapper.like("mobile",tbMenberVo.getMobile());
        }
        // 模糊查询,昵称 nickname是否有值,如果有就加上这个条件
        if(StringUtils.hasText(tbMenberVo.getNickname())){
            wrapper.like("nickname",tbMenberVo.getNickname());
        }
        // 时间区间查询,判断是有索引是否有值 如果就加上
        if (tbMenberVo.getGmtCreate().size()>0){
            if (tbMenberVo.getGmtCreate().get(0)!=null&&tbMenberVo.getGmtCreate().get(1)!=null){
                List<String> gmtCreate = tbMenberVo.getGmtCreate();
                String s = gmtCreate.get(0);
                if(StringUtils.hasText(s)){
                    wrapper.ge("gmt_create", s);
                }
                String a = tbMenberVo.getGmtCreate().get(1);
                if(StringUtils.hasText(a)){
                    wrapper.le("gmt_create",a);
                }
            }
        }
        //  状态查询 判断是有值 , 如果有就mysql语句就加上
        if (ObjectUtil.isNotEmpty(tbMenberVo.getIsDisabled())){
            wrapper.eq("is_disabled",tbMenberVo.getIsDisabled());
        }
        IPage<TbMember> iPage =  tbMenberDao.selectPage(page,wrapper);
        return new Result<>(2000,"查询成功",iPage);
    }

    /**
     * 编辑
     * @param upTbMenberVo 编辑内容
     * @return
     */
    @Override
    public Result<TbMember> UPdateTbMenber(UpTbMenberVo upTbMenberVo) {
        tbMenberDao.UpdateTbMenber(upTbMenberVo);
        return new Result<>(2000,"编辑成功");
    }

    /**
     * 状态更改
     * @param id 会员id
     * @param isDisabled 状态
     * @return
     */
    @Override
    public Result UPdateTbMenberState(String id, Integer isDisabled) {
        tbMenberDao.UpdateTbMenberState(id,isDisabled);
        return new Result(2000,"更改完成");
    }

    /**
     * @create by: Username 刘亦辰 
     * @description: 前端用户注册 
     * @create time: 2022/12/17 16:35
     * @param null
     * @return :  
     **/
    @Override
    public Result installMember(String mobile, String password) {
        Map<String, String> stringMap = RandomCode.makePasswordSalt(password);
        TbMember tbMember = new TbMember();
        tbMember.setMobile(mobile);
        tbMember.setPassword(stringMap.get("password"));
        tbMember.setNickname(mobile);
        tbMember.setIsDeleted(0);
        tbMember.setIsDisabled(0);
        tbMember.setGmtCreate(new Date());
        tbMember.setGmtModified(new Date());
        tbMember.setIsType(0);
        tbMember.setSalt(stringMap.get("salt"));
        int insert = tbMenberDao.insert(tbMember);
        if (insert>0){
            return  new Result(2000,"用户创建成功");
        }
        return new Result(0000,"用户创建失败");
    }


    /**
     * @create by: Username 刘亦辰 
     * @description: 前台登录 
     * @create time: 2022/12/17 17:18
     * @param null
     * @return :  
     **/
    @Override
    public Result qianLogin(QianLoginVo qianLoginVo) {
        TbMember member = tbMenberDao.queryMemberByMobile(qianLoginVo.getMobile());
        if (ObjectUtil.isNotEmpty(member)){
            String password = member.getPassword();
            String salt = member.getSalt();
            Map  tokenMap = new HashMap();
            tokenMap.put("mobile",member.getMobile());
            tokenMap.put("password",member.getPassword());
            tokenMap.put("type",member.getIsType());
            String s = tbMenberDao.checkPassword(qianLoginVo.getMobile());
            if (s.equals(password)){
                Map map = new HashMap();
                String homeToken = JwtTool.createJWT(tokenMap);
                map.put("homeToken",homeToken);
                map.put("mobile",member.getMobile());
                map.put("memberId",member.getId());
                if (member.getIsType().equals(1)){
                    map.put("isTeacher","1");
                    return  new Result(2000,"登录成功",map);
                }
                map.put("isTeacher","0");
                return  new Result(2000,"登录成功",map);
            }
        }
        return new Result(0000,"用户“"+qianLoginVo.getMobile()+"”不存在");
    }

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */

    @Override
    public TbMember selectQueryAllId(String id) {
        return tbMenberDao.selectQueryAllId(id);
    }

    /**
     * 根据手机号查询用户信息
     * @param mobile 手机号
     * @return
     */
    @Override
    public TbMember selectQueryAllMobile(String mobile) {
        return tbMenberDao.selectQueryAllMobile(mobile);
    }

    /**
     * 根据手机号修改用户密码
     * @param mobile 手机号
     * @param newPassword  密码
     * @return
     */

    @Override
    public Result UPdateTbMenberPassword(String mobile, String newPassword) {
        System.out.println("===="+newPassword);
        Map<String, String> stringMap = RandomCode.makePasswordSalt(newPassword);
        System.out.println("------------------"+stringMap);
//        TbMember tbMember = new TbMember();
//        tbMember.setMobile(mobile);
//        tbMember.setPassword(stringMap.get("newPassword"));
        int i = tbMenberDao.updatePassword(mobile,stringMap.get("password"));
        if (i > 0){
            return new Result(2000,"修改成功");
        }
        return new Result(1000,"修改失败");
    }

    //
    //@Autowired
    //private RedisUtils redisUtils;
    //
    ///**
    // * 发送验证码
    // * @param phone
    // * @return
    // */
    //@Override
    //public String sendCode(String phone) {
    //    // 查询redis中的验证码是否过期
    //    String code = redisUtils.get(phone);
    //    if (StringUtils.hasText(code)) {
    //        return "验证码尚未过期，验证码为：" + code;
    //    }
    //
    //    code = String.valueOf((int)((Math.random()*9+1)*1000));
    //    try {
    //        /**
    //         * 发送验证码给用户
    //         * phone 接受验证码的手机号
    //         * templateCode 短信模板id
    //         * code 验证码
    //         */
    //        CodeUtils.sendCode(phone, code);
    //        redisUtils.set(phone, code, 5);
    //        return "验证码发送成功:" + code;
    //    } catch (Exception e) {
    //        e.printStackTrace();
    //        return "验证码发送失败";
    //    }
    //}
    //
    ///**
    // * 查询缓存中的验证码
    // * @param phone
    // * @return
    // */
    //@Override
    //public String getCode(String phone) {
    //    return redisUtils.get(phone);
    //}
}
